package ckw.gui.wizards;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Random;

import org.eclipse.jface.wizard.Wizard;

import ckw.database.DatabaseManager;
import ckw.database.Election;
import ckw.database.OKW;
import ckw.database.OKWMember;
import ckw.database.VotersList;
import ckw.database.VotingCard;
import ckw.database.VotingSubcard;
import ckw.database.VotingSubcardOption;
import ckw.gui.MainWindowC;

// TODO: nie ma walidacji pol i kiedy mozna robic finish/next/back itd.
public class WzdElection extends Wizard {
	private WzdElectionPg1 pg1;
	private WzdElectionPg2 pg2;
	private WzdElectionPg3 pg3;
	private WzdElectionPg4 pg4;

	
	public WzdElection() {
		setWindowTitle("Dodaj wybory");
		
		pg1 = new WzdElectionPg1();
		pg2 = new WzdElectionPg2();
		pg3 = new WzdElectionPg3();
		pg4 = new WzdElectionPg4();
	}

	@Override
	public void addPages() {
		addPage(pg1);
		addPage(pg2);
		addPage(pg3);
		addPage(pg4);
	}

	@Override
	public boolean performFinish() {
		DatabaseManager db = DatabaseManager.getInstance();
		
		// Pobieramy dane ze stron wizardow:
		String name = pg1.getElectionName();
		Timestamp startDate = pg1.getElectionStartTime();
		Timestamp endTime = pg1.getElectionEndTime();
		
		ArrayList<OKW> selectedOkwList = pg2.getElectionOkwList();
		Random r = new Random();
		String token;
		//ArrayList<OKWMember> selectedOKWMemberList = new ArrayList<OKWMember>();
		
		ArrayList<VotersList> selectedVotersList = pg3.getElectionVotersListList();
		
		ArrayList<VotingCard> selectedVotingCards = pg4.getElectionVotingCardList();

		Election election = new Election(name, startDate, endTime);
		db.save(election);
		// Zapisujemy do bazy tylko te elementy, ktore sa checked (czyli zostaly odhaczone w wizardach)
		for (OKW okw : selectedOkwList) {
			if(okw.getChecked() == true) {
				db.addOKWToElection(okw, election);
				for (OKWMember okwMember : okw.getOkwMembers()) {
					if(okwMember.getChecked() == true) {
						System.out.println("!");
						// generacja tokenu administracyjnego
						Integer l = r.nextInt(100000);
						token = new String(l.toString());
						db.saveAdministratorToken(token, okwMember, election);
					}
				}
			}
		}
		for (VotersList votersList : selectedVotersList) {
			if(votersList.getChecked() == true) {
				db.addVoterListToElection(votersList, election);
			}
		}
		for (VotingCard votingCard : selectedVotingCards) {
			votingCard.addToElection(election);
			db.save(votingCard);
			for (VotingSubcard votingSubcard : votingCard.getVotingSubcards()) {
				votingSubcard.addToVotingCard(votingCard);
				db.save(votingSubcard);
				for (VotingSubcardOption votingSubcardOption : votingSubcard
						.getVotingSubcardOptions()) {
					votingSubcardOption.addToVotingSubcard(votingSubcard);
					db.save(votingSubcardOption);
				}
			}
		}
		db.update(election);
		MainWindowC.aktualizujWybory();
		return true;
	}
	
	public boolean canFinish() {
		if(getNextPage(getContainer().getCurrentPage()) == null) {
			return true;
		} else {
			return false;
		}
	}
}
